package com.smz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.CustomerDAO;
import com.smz.dao.ParDAO;
import com.smz.dao.SCompanyDAO;
import com.smz.dao.TRelationDAO;
import com.smz.entity.Customer;
import com.smz.entity.SCompany;
import com.smz.entity.TPar;
import com.smz.entity.TRelation;
import com.smz.service.TParService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class TParServiceImpl extends ServiceImpl<ParDAO, TPar> implements TParService {
    @Autowired
    ParDAO parDAO;
    @Autowired
    TRelationDAO tRelationDAO;
    @Autowired
    CustomerDAO customerDAO;
    @Autowired
    SCompanyDAO sCompanyDAO;

    @Override
    public String addTPar(int kid) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SCompany sCompany = sCompanyDAO.selectCompanyByLegal(principal.getUsername());
        int sid=sCompany.getCompanyId();
        TRelation tRelation = tRelationDAO.selectRelation(sid, kid);
        String i ="1";
        if(tRelation==null){
            Customer customer = customerDAO.selectById(sid);
            if(customer==null){
                i="0";
            }
            String id1 =String.valueOf( sid);
            String id2 =String.valueOf( kid);
            Date date=new Date();
            Timestamp timestamp=new Timestamp(date.getTime());
            TPar par = new TPar("未处理","未审核",timestamp ,"暂无",id1,id2,i);
            parDAO.insert(par);
            return "ok";
        }else {
            return "error";
        }
    }

    @Override
    public IPage<TPar> findTParByB(int current, int size) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SCompany sCompany = sCompanyDAO.selectCompanyByLegal(principal.getUsername());
        int id=sCompany.getCompanyId();
        return parDAO.selectTParByB(new Page(current,size),String.valueOf(id));
    }

    @Override
    public IPage<TPar> findTParByA(int current, int size) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SCompany sCompany = sCompanyDAO.selectCompanyByLegal(principal.getUsername());
        int id=sCompany.getCompanyId();
        return parDAO.selectTParByA(new Page(current,size),String.valueOf(id));
    }

    @Override
    public IPage<TPar> findTParByTime(int current, int size, String time1, String time2) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SCompany sCompany = sCompanyDAO.selectCompanyByLegal(principal.getUsername());
        int id=sCompany.getCompanyId();
        return parDAO.selectTParByTime(new Page(current,size),time1,time2,String.valueOf(id));
    }

    @Override
    public IPage<TPar> findAll(int current, int size) {
        return parDAO.selectAll(new Page(current,size));
    }
}
